const getOpenId = require('./getOpenId/index');
const getMiniProgramCode = require('./getMiniProgramCode/index');
const createCollection = require('./createCollection/index');
const selectRecord = require('./selectRecord/index');
const updateRecord = require('./updateRecord/index');
const sumRecord = require('./sumRecord/index');

const tcp = wx.createTCPSocket()
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ip:'192.168.4.1',
    dk:'10500',
    word:'on37',
    ys:'40',
    state:'未连接',
    wifissid: '' // WIFI ssid
  },
 
  onLoad: function (e) {  // 获取参数
    var that = this;
    wx.startWifi({
      success (res) {
        console.log(res.errMsg)
      }
    })
    wx.getConnectedWifi({
      success: function(e){
        that.setData({
          wifissid: e.wifi.SSID,
        })
        console.log("wifiyes");
      },
      fail:function(e){
        console.log("wififail"+JSON.stringify(e));
      }
 
    })
  },
 
  ipInput:function(e){
    this.setData({ip:e.detail.value})
  },
  dkInput:function(e){
    this.setData({dk:e.detail.value})
  },
  wordInput:function(e){
    this.setData({word:e.detail.value})
  },
  ysInput:function(e){
    this.setData({ys:e.detail.value})
  },
  loginBtnClick: function () {
    console.log("连接");
    tcp.connect({address: this.data.ip, port: this.data.dk})
 
    this.setData({
      state: '已连接'
    })
  },
  sendClick: function () {
    console.log("发送："+this.data.word+' '+this.data.ys);
    tcp.write(this.data.word+' '+this.data.ys)
    
    this.setData({
      state: '已发送'
    })
  },
 
  oneClick: function () {
 
    console.log("连接1");
    tcp.connect({address: this.data.ip, port: this.data.dk})
    setTimeout(function () {
    console.log("发送1");}, 50)
    tcp.write(this.data.word+' '+this.data.ys)
 
    setTimeout(function () {
    console.log("关闭连接1");}, 100)
    tcp.close()
 
    setTimeout(function () {
      console.log("连接2");}, 200)
    tcp.connect({address: this.data.ip, port: this.data.dk})
    setTimeout(function () {
    console.log("发送2");}, 250)
    tcp.write(this.data.word+' '+this.data.ys)
    setTimeout(function () {
    console.log("关闭连接2");}, 300)
    tcp.close()
    this.setData({
    state: '已关闭'
    })
  },
  offClick:function () {
    console.log("断开");
    tcp.close()
    this.setData({
      state: '已关闭'
    })
  }
 
})

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.type) {
    case 'getOpenId':
      return await getOpenId.main(event, context);
    case 'getMiniProgramCode':
      return await getMiniProgramCode.main(event, context);
    case 'createCollection':
      return await createCollection.main(event, context);
    case 'selectRecord':
      return await selectRecord.main(event, context);
    case 'updateRecord':
      return await updateRecord.main(event, context);
    case 'sumRecord':
      return await sumRecord.main(event, context);
  }
};
